﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using QLDBHYT.BUS;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
namespace QLDBHYT
{
    public partial class frmKCBNoitruDetail : Form
    {
        private Modules.ClickButtonStatus flagButton = Modules.ClickButtonStatus.Ngung;//co phan biet dang o trang thai ghi hay ngung
        public P2hVO p2hVO;
        public SttVO sttVO;
        private List<P2dVO> lstP2dVO;
        private SttDAO sttDAO;
        
        PhieunhapBUS phieunhapBUS;
        private P2hBUS p2hBUS;
        public frmKCBNoitruDetail()
        {
            InitializeComponent();
        }
        private void frmKCBNoitruDetail_Load(object sender, EventArgs e)
        {
            p2hBUS = new P2hBUS();
            phieunhapBUS = new PhieunhapBUS();
            sttDAO = new SttDAO();
            lstP2dVO = new List<P2dVO>();
            this.Text = this.Text + " - " + p2hVO.Sophieu;
            txtNgayungthuoc.Show();
            txtNgayungthuoc.Value = DateTime.Now;
            GetThuoc();
            FormatFa();
            GetListNgay();
        }
        private void GetListNgay()
        {
            DataTable tbNgay = p2hBUS.GetListP2d_Detail(p2hVO.Ctid);
            faNgay.Rows.Count = 1;
            int i = 1;
            foreach (DataRow p2dvo in tbNgay.Rows)
            {
                faNgay.Rows.Count = faNgay.Rows.Count + 1;
                faNgay.Rows[i]["Ngay"] = p2dvo["Ngay"];
                if (Convert.ToDateTime(faNgay.Rows[i]["Ngay"]).ToShortDateString() == Convert.ToDateTime(txtNgayungthuoc.Value).ToShortDateString())
                {
                    faNgay.Select(i, 1);
                }
                i++;
            }
        }
        private void GetListNgay(string ngay)
        {
            lstP2dVO = p2hBUS.GetListP2d_Detail(p2hVO.Ctid, Convert.ToDateTime(ngay));
            fa.Rows.Count = 1;
            int i = 1;
            foreach (P2dVO p2dvo in lstP2dVO)
            {
                fa.Rows.Count = fa.Rows.Count + 1;
                fa.Rows[i]["manhomcp"] = p2dvo.Sttnh;
                fa.Rows[i]["tencp"] = p2dvo.Tenhieu.Trim();
                fa.Rows[i]["mahieu"] = p2dvo.Mahieu;
                fa.Rows[i]["ktc"] = p2dvo.Ktc;
                fa.Rows[i]["sl"] = p2dvo.Sl;
                fa.Rows[i]["slcu"] = p2dvo.Sl;
                fa.Rows[i]["gia"] = p2dvo.Gia;
                fa.Rows[i]["giabhxh"] = p2dvo.Giabhxh;
                fa.Rows[i]["tongtien"] = p2dvo.Tongtien;
                fa.Rows[i]["Tienbhxh"] = p2dvo.Tienbhxh;
                fa.Rows[i]["dvt"] = p2dvo.Dvt;
                fa.Rows[i]["tienbn"] = p2dvo.Tien_bn;
                switch (fa.Rows[i]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[i]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[i]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[i]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[i]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[i]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[i]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[i]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[i]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[i]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[i]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[i]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[i]["NhomCP"] = "Thuốc K";
                        break;
                }
                i++;

            }
        }
        private List<ThuocVO> GetTenHieu(int thang, int nam)
        {

            List<ThuocVO> lstThuocVO = new List<ThuocVO>();
            ThuocVO thuocVO;
            DataTable dataTable = new DataTable();
            P2hDAO p2hDAO = new P2hDAO();
            dataTable = p2hDAO.SearchTenHieu(thang, nam);
            foreach (DataRow dr in dataTable.Rows)
            {
                if (!(Convert.ToInt32(dr["slcuoiky"]) == 0 && (dr["stt_nh"].ToString().Trim() == "01"
                    || dr["stt_nh"].ToString().Trim() == "03")))
                {
                    thuocVO = new ThuocVO();
                    thuocVO.Sttnh = dr["stt_nh"].ToString().Trim();
                    thuocVO.Mahieu = dr["Ma_hieu"].ToString().Trim();
                    thuocVO.Tenhieu = dr["ten_hieu"].ToString().Trim();
                    thuocVO.Dvt = dr["dvt"].ToString();
                    thuocVO.Gia = Convert.ToDecimal(dr["gia"]);
                    thuocVO.Soluong = Convert.ToInt32(dr["slcuoiky"]);
                    thuocVO.Giabhxh = Convert.ToDecimal(dr["gia_bhxh"]);
                    thuocVO.Ktc = dr["ktc"].ToString();
                    lstThuocVO.Add(thuocVO);
                }
            }
            return lstThuocVO;
        }
        private void FormatFa()
        {
            fa.Cols[2].Editor = cboThuoc;
            fa.Cols[2].Editor.Width = 800;
        }
        private void GetThuoc()
        {
            cboThuoc.ClearItems();
            cboThuoc.AllowSort = true;
            cboThuoc.DataSource = GetTenHieu(Convert.ToInt32(Modules.GetKy().Rows[0]["thang"]),
               Convert.ToInt32(Modules.GetKy().Rows[0]["nam"]));
            cboThuoc.Splits[0].DisplayColumns[0].Width = 400;
            cboThuoc.Splits[0].DisplayColumns[1].Width = 50;
            cboThuoc.Splits[0].DisplayColumns[2].Width = 50;

        }
        private void ClearControl(bool flag)
        {
            txtNgayungthuoc.Value  = DateTime.Now;
            fa.Rows.Count = 1;
            
        }
        private bool CheckSoLuongTon()
        {
            DataTable tbPhieuNhap;
            string condition;
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null && (fa.Rows[i]["manhomcp"].ToString() == "01" || fa.Rows[i]["manhomcp"].ToString() == "03"))
                {
                    condition = " and a.ma_hieu = '" + fa.Rows[i]["mahieu"].ToString() + "'";
                    tbPhieuNhap = phieunhapBUS.GetALL(Convert.ToInt32(Modules.thang), Modules.nam, condition);
                    if (tbPhieuNhap.Rows.Count == 0) { return false; }
                    if (Convert.ToInt32(tbPhieuNhap.Rows[0]["slcuoiky"]) + Convert.ToInt16(fa.Rows[i]["slcu"]) < Convert.ToInt16(fa.Rows[i]["sl"]))
                    {
                        MessageBox.Show("Số lượng thuốc " + fa.Rows[i]["TenCP"].ToString() + " còn tồn " + tbPhieuNhap.Rows[0]["slcuoiky"]);
                        return false;
                    }


                }
            }
            return true;

        }
        private void SetValueToP2D(SttVO sttVO)
        {
            P2dVO p2dVO;

            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {

                    p2dVO = new P2dVO();
                    p2dVO.Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
                    p2dVO.Ctid0 = string.Format("{0:000000000000000}", sttDAO.GetStt_Ct_id2_Noi(Modules.nam));
                    p2dVO.Sttnh = fa.Rows[i]["manhomcp"].ToString();
                    p2dVO.Mahieu = fa.Rows[i]["mahieu"].ToString();
                    p2dVO.Tenhieu = fa.Rows[i]["tencp"].ToString();
                    p2dVO.Ngay = txtNgayungthuoc.Text.ToString().Substring(6, 4) + "/" + txtNgayungthuoc.Text.ToString().Substring(3, 2) + "/" + txtNgayungthuoc.Text.ToString().Substring(0, 2); 
                    p2dVO.Tyle = 100;
                    p2dVO.Ktc = fa.Rows[i]["ktc"].ToString();
                    p2dVO.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                    p2dVO.Gia = Convert.ToDecimal(fa.Rows[i]["gia"]);
                    p2dVO.Giabhxh = Convert.ToDecimal(fa.Rows[i]["giabhxh"]);
                    p2dVO.Tongtien = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                    p2dVO.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                    p2dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                    p2dVO.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                    p2dVO.Dvt = fa.Rows[i]["dvt"].ToString();
                    p2dVO.Loai = 1;
                    p2dVO.Bacsy = "";
                    p2dVO.Khoa = "";
                    p2dVO.Chon = 0;
                    p2dVO.Flag = 3;
                    lstP2dVO.Add(p2dVO);
                    sttVO.Ctid0++;
                }
            }

        }
        private void UpdateFA()
        {
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {
                    fa.Rows[i]["slcu"] = fa.Rows[i]["sl"];
                }
            }
        }
        private void CheckUpdateP2D()
        {
            P2dVO p2dVO;
            bool flag = false;
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null &&
                    fa.Rows[i]["mahieu"].ToString() != "" && fa.Rows[i]["tencp"] != ""
                    && fa.Rows[i]["sl"] != null)
                {
                    flag = false;
                    foreach (P2dVO item in lstP2dVO)
                    {
                        //so khong thay doi
                        if (item.Mahieu == fa.Rows[i]["mahieu"].ToString())
                        {
                            if (item.Sl == Convert.ToInt16(fa.Rows[i]["sl"]))
                            {
                                item.Flag = 2;
                                item.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                                item.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                                flag = true;
                                break;
                            }
                            else //so luong thay doi
                            {

                                item.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                                item.Tongtien = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                                item.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                                item.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                                item.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                                item.Flag = 2;
                                flag = true;
                                break;
                            }
                        }
                    }
                    // them moi du lieu
                    if (flag == false)
                    {
                        p2dVO = new P2dVO();
                        p2dVO.Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
                        p2dVO.Ctid0 = string.Format("{0:000000000000000}", sttDAO.GetStt_Ct_id2_Noi(Modules.nam));
                        p2dVO.Sttnh = fa.Rows[i]["manhomcp"].ToString();
                        p2dVO.Mahieu = fa.Rows[i]["mahieu"].ToString();
                        p2dVO.Tenhieu = fa.Rows[i]["tencp"].ToString();
                        p2dVO.Tyle = 100;
                        p2dVO.Ktc = fa.Rows[i]["ktc"].ToString();
                        p2dVO.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                        p2dVO.Gia = Convert.ToDecimal(fa.Rows[i]["gia"]);
                        p2dVO.Giabhxh = Convert.ToDecimal(fa.Rows[i]["giabhxh"]);
                        p2dVO.Tongtien = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                        p2dVO.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                        p2dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                        p2dVO.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                        p2dVO.Dvt = fa.Rows[i]["dvt"].ToString();
                        p2dVO.Ngay = faNgay.Rows[faNgay.Selection.TopRow]["Ngay"].ToString();
                        p2dVO.Loai = 1;
                        p2dVO.Bacsy = "";
                        p2dVO.Khoa = "";
                        p2dVO.Chon = 0;
                        p2dVO.Flag = 3;
                        lstP2dVO.Add(p2dVO);
                        sttVO.Ctid0++;
                    }


                }

            }
        }
        public void btnThêm_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            switch (flagButton)
            {
                case Modules.ClickButtonStatus.Ngung:
                    flagButton = Modules.ClickButtonStatus.Them;
                    ClearControl(false);
                    faNgay.Enabled = false;
                    fa.Enabled = true;
                    break;
                case Modules.ClickButtonStatus.Them:
                    if (CheckSoLuongTon() == false) {
                        this.Cursor = Cursors.Default;
                        return; }
                    lstP2dVO.Clear();
                    SetValueToP2D(sttVO);
                    p2hBUS.UpdateRecodrP2D_Detail(lstP2dVO);
                    GetThuoc();
                    UpdateFA();
                    GetListNgay();
                    flagButton = Modules.ClickButtonStatus.Sua;
                    break;

                case Modules.ClickButtonStatus.Sua:
                    if (CheckSoLuongTon() == false) {
                        this.Cursor = Cursors.Default;
                        return; }
                    CheckUpdateP2D();
                    p2hBUS.UpdateRecodrP2D_Detail(lstP2dVO);
                    GetThuoc();
                    UpdateFA();
                   
                    break;
            }
            StatusButton();
            this.Cursor = Cursors.Default;
        }
        private void StatusButton()
        {
            if (flagButton == Modules.ClickButtonStatus.Them || flagButton == Modules.ClickButtonStatus.Sua)
            {
                btnThem.Text = "&Ghi";
                btnSua.Text = "&Ngưng";
                btnXoa.Enabled = false;
                btnThoat.Enabled = false;
               

            }
            else
            {
                btnThem.Text = "&Thêm";
                btnSua.Text = "&Sửa";
                btnXoa.Enabled = true;
                btnThoat.Enabled = true;
                
            }
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            switch (flagButton)
            {
                case Modules.ClickButtonStatus.Ngung:

                    flagButton = Modules.ClickButtonStatus.Sua;
                    faNgay.Enabled = false;
                    fa.Enabled = true;
                    break;
                case Modules.ClickButtonStatus.Sua:
                    faNgay.Enabled = true;
                    fa.Enabled = false;
                    flagButton = Modules.ClickButtonStatus.Ngung;
                    break;
                case Modules.ClickButtonStatus.Them:

                    flagButton = Modules.ClickButtonStatus.Ngung;
                    faNgay.Enabled = true;
                    fa.Enabled = false;
                    break;

            }
            StatusButton();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void fa_AfterEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            if (fa.Cols[e.Col].Name == "SL")
            {
                fa.Rows[e.Row]["tongtien"] = Convert.ToInt32(fa.Rows[e.Row]["SL"]) * Convert.ToDecimal(fa.Rows[e.Row]["gia"]);
                
            }
            if (fa.Cols[e.Col].Name == "TenCP" && fa.Rows[e.Row]["TenCP"].ToString().Trim() != "")
            {
                for (int i = 1; i < fa.Rows.Count; i++)
                {
                    if (e.Row != i && fa.Rows[i]["mahieu"] != null && fa.Rows[e.Row]["TenCP"].ToString().Trim() == fa.Rows[i]["TenCP"].ToString().Trim())
                    {
                        MessageBox.Show("Mã hiệu bị trùng vui lòng nhập lại");
                        fa.Rows[e.Row]["TenCP"] = "";
                        return;
                    }

                }
                fa.Rows[e.Row]["gia"] = cboThuoc.Columns["gia"].Value;
                fa.Rows[e.Row]["giabhxh"] = cboThuoc.Columns["giabhxh"].Value;
                fa.Rows[e.Row]["MaNhomCP"] = cboThuoc.Columns["sttnh"].Value;
                fa.Rows[e.Row]["mahieu"] = cboThuoc.Columns["mahieu"].Value;
                fa.Rows[e.Row]["ktc"] = cboThuoc.Columns["ktc"].Value;
                fa.Rows[e.Row]["dvt"] = cboThuoc.Columns["dvt"].Value;
                if (fa.Rows[e.Row]["SL"] != null)
                {
                    fa.Rows[e.Row]["tongtien"] = Convert.ToInt32(fa.Rows[e.Row]["SL"]) * Convert.ToDecimal(fa.Rows[e.Row]["gia"]);
                }
                switch (fa.Rows[e.Row]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[e.Row]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[e.Row]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[e.Row]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[e.Row]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[e.Row]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[e.Row]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[e.Row]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[e.Row]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[e.Row]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[e.Row]["NhomCP"] = "Thuốc K";
                        break;
                }
            }
        }
        private void frmKCBNoitruDetail_KeyDown(object sender, KeyEventArgs e)
        {
            Modules.SendKey(this, e);
            if (e.KeyCode == Keys.F11)
            {
                fa.Focus();
                fa.Select(fa.Rows.Count - 1, 2);
                fa.StartEditing(fa.Rows.Count - 1, 2);

            }
        }
        private void faNgay_AfterRowColChange(object sender, C1.Win.C1FlexGrid.RangeEventArgs e)
        {
            if (faNgay.Selection.TopRow > 0 && faNgay.Rows[faNgay.Selection.TopRow]["Ngay"] != null)
            {
                GetListNgay(faNgay.Rows[faNgay.Selection.TopRow]["Ngay"].ToString());
            }
        }
        private void fa_KeyUp(object sender, KeyEventArgs e)
        {
            if (fa.Selection.c1 == 4 && e.KeyCode == Keys.Enter)
            {
                fa.Select(fa.Selection.r1 + 1, 2);
                fa.StartEditing(fa.Selection.r1, 2);

            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            p2hBUS.DeleteRecordP2d_Detail(p2hVO.Ctid, Convert.ToDateTime(faNgay.Rows[faNgay.Selection.TopRow]["Ngay"].ToString()));
            fa.Rows.Count = 1;
            GetListNgay();
            this.Cursor = Cursors.Default;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            int stt = 0;
            string strTennhom = "cong kham";
            DataRow newRow = null;
            Dataset.dsBCNX dsBCNX = new Dataset.dsBCNX();
            newRow = dsBCNX.Tables["P1H"].NewRow();
            newRow["sophieu"] = p2hVO.Sophieu;
            newRow["hoten"] = Converter.TCVN3ToUnicode(p2hVO.Hoten);
            newRow["namsinh"] = p2hVO.Namsinh;
            newRow["lenhdx"] = Converter.TCVN3ToUnicode(p2hVO.Noiden);
            newRow["gioitinh"] = p2hVO.Gioitinh;
            newRow["diachi"] = Converter.TCVN3ToUnicode(p2hVO.Diachi);
            newRow["mathe"] = p2hVO.Mathe;
            newRow["gttu"] = Convert.ToDateTime(p2hVO.Gttu).ToString().Substring(0, 10);
            newRow["gtden"] = Convert.ToDateTime(p2hVO.Gtden).ToString().Substring(0, 10);
            newRow["makcb"] = p2hVO.Makcb;
            newRow["tinhkcb"] = p2hVO.Tinhkcb;
            newRow["noikham"] = Converter.TCVN3ToUnicode(p2hVO.Noikham);
            newRow["ngayvao"] = Convert.ToDateTime(p2hVO.Ngayvao).ToString().Substring(0, 10);
            newRow["ngayra"] = Convert.ToDateTime(p2hVO.Ngayra).ToString().Substring(0, 10);
            newRow["tongngay"] = (Convert.ToDateTime(p2hVO.Ngayra) - Convert.ToDateTime(p2hVO.Ngayvao)).Days + 1;
            newRow["ngaytt"] = Convert.ToDateTime(p2hVO.Ngaytt).ToString().Substring(0, 10);
            newRow["dieutri"] = p2hVO.Dieutri;
            newRow["chuandoan"] = Converter.TCVN3ToUnicode(p2hVO.Tenbenh);
            newRow["ngay"] = Convert.ToDateTime(faNgay.Rows[faNgay.Selection.TopRow]["Ngay"].ToString()).ToString().Substring(0, 10);
            if (p2hVO.Mathe != "")
            {
                newRow["SOKCB11"] = p2hVO.Mathe.Substring(0, 2);
                newRow["SOKCB12"] = p2hVO.Mathe.Substring(2, 1);
                newRow["SOKCB13"] = p2hVO.Mathe.Substring(3, 2);
                newRow["SOKCB14"] = p2hVO.Mathe.Substring(5, 2);
                newRow["SOKCB15"] = p2hVO.Mathe.Substring(7, 3);
                newRow["SOKCB16"] = p2hVO.Mathe.Substring(10);
            }
            newRow["fullname"] = Properties.Settings.Default.FullName;
            newRow["ketoan"] = Properties.Settings.Default.Ketoan;
            newRow["doituong"] = p2hVO.Doituong;

            dsBCNX.Tables["P1H"].Rows.Add(newRow);

            
            var q = lstP2dVO.OrderBy(o => o.Sttnh);
            foreach (P2dVO p2dvo in q)
            {
                newRow = dsBCNX.Tables["P1D"].NewRow();
                if (p2dvo.Sttnh != strTennhom)
                {
                    stt++;
                    strTennhom = p2dvo.Sttnh;
                }
                newRow["nhomcp"] = stt;
                newRow["tencp"] = Converter.TCVN3ToUnicode(p2dvo.Tenhieu);
                newRow["dvt"] = p2dvo.Dvt;
                newRow["sl"] = p2dvo.Sl;
                newRow["gia"] = p2dvo.Gia;
                newRow["tongtien"] = p2dvo.Tongtien;
                newRow["Tienbhxh"] = p2dvo.Tienbhxh;
                newRow["tienbn"] = p2dvo.Tien_bn;
                switch (p2dvo.Sttnh.ToString())
                {
                    case "01":
                        newRow["tennhomcp"] = "Trong danh mục";
                        break;
                    case "02":
                        newRow["tennhomcp"] = "Máu, Đạm";
                        break;
                    case "03":
                        newRow["tennhomcp"] = "Dịch truyền";
                        break;
                    case "04":
                        newRow["tennhomcp"] = "Xét nghiệm";
                        break;
                    case "05":
                        newRow["tennhomcp"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        newRow["tennhomcp"] = "Thủ thuật";
                        break;
                    case "09":
                        newRow["tennhomcp"] = "DVKT cao";
                        break;
                    case "10":
                        newRow["tennhomcp"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        newRow["tennhomcp"] = "Ngày giường";
                        break;
                    case "12":
                        newRow["tennhomcp"] = "Vận chuyển";
                        break;
                    case "13":
                        newRow["tennhomcp"] = "VTYT thay thế";
                        break;
                    case "14":
                        newRow["tennhomcp"] = "Thuốc K";
                        break;
                }

                dsBCNX.Tables["P1D"].Rows.Add(newRow);
            }
            Cursor.Current = Cursors.Default;
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptKCBNoitruDetail.rpt");
            cr.SetDataSource(dsBCNX);
            f.crpView.ReportSource = cr;
            Button bt = (Button)sender;
            if (bt.Name == "btnIn")
            {
                f.crpView.PrintReport();
            }
            else
            {
                f.Show();
            }
        }
            
    }
}
